//
//  NewsDetailViewModel.m
//  xinhua_sx
//
//  Created by 党阳阳 on 14-5-27.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "NewsDetailViewModel.h"
#import "XHURLString.h"


@interface NewsDetailViewModel()

@property (nonatomic ,strong) XHURLString *urlString;
@property (nonatomic ,strong) id newsId;

@property (nonatomic ,strong) NSString *attentionURL;
@property (nonatomic ,strong) NSString *deleteAttentionURL;
@property (nonatomic ,strong) NSString *praiseURL;
@property (nonatomic ,strong) NSString *newsDetailString;
@end

@implementation NewsDetailViewModel
- (instancetype) initWithNewsId:(id)newId
{
    self = [super init];
    if (self) {
        self.urlString = [[XHURLString alloc] init];
        self.newsId = newId;
        
    }
    
    return  self;
}


- (NSURL *)getNewsURL
{
    return [NSURL URLWithString:[self.urlString getNewsDetailWihtNewsId:self.newsId]];
    
}
- (RACSignal *)deleteAttentionNewsWithUserId:(id)userId NewsId:(id)newsId
{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSString* stinrg = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/articlesDelete-%@-%@.html",userId,self.newsId];

        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stinrg]];
        [urlRequest setHTTPMethod:@"GET"];
        [urlRequest setTimeoutInterval:10];
        
        NSOperationQueue *queue= [[NSOperationQueue alloc] init];
        
        [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            
            if ([data length]>0 && connectionError == nil) {
                
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&connectionError];
               
               
                if (dic) {
                    [subscriber sendNext:dic];
                    
                
                }else{
                    [subscriber sendNext:@"网络错误"];
                }
                    
            }
            if (connectionError) {
                [subscriber sendNext:@"网络错误"];
                
            }
            
                
        }];
        
        return nil;
    }];
    
}
            
- (RACSignal *)attentionNewsWithUserId:(id)userId NewsId:(id)newsId
{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    
       
        
    
        NSString* stinrg = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/articlesSave-%@-%@.html",userId,self.newsId];
        NSLog(@"收藏url = %@",stinrg);
        NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stinrg]];
        [urlRequest setHTTPMethod:@"GET"];
        [urlRequest setTimeoutInterval:10];

        NSOperationQueue *queue= [[NSOperationQueue alloc] init];

        [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

            
            if ([data length]>0 && connectionError == nil) {
                
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&connectionError];
                if (dic) {
                    [subscriber sendNext:dic];
//                    NSLog(@"%@",dic);
                }else{
                    [subscriber sendNext:@"网络错误"];

                }
                
            }
            if (connectionError) {
                [subscriber sendNext:@"网络错误"];

            }

        }];

        

         return nil;
    }];
   
}

- (RACSignal *)praiseNewsWitdUserId:(id)userId NewsId:(id)newsId
{
     return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    
     
         NSString* stinrg = [NSString stringWithFormat:@"http://183.203.18.45:7001/xinhua/m/praiseSave-%@-%@.html",userId,self.newsId];
         NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:stinrg]];
         [urlRequest setHTTPMethod:@"GET"];
         [urlRequest setTimeoutInterval:10];
    
         NSOperationQueue *queue= [[NSOperationQueue alloc] init];
    
         [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
             if ([data length]>0 && connectionError == nil) {
            
                 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&connectionError];
            
                 if (dic) {
                
                     [subscriber sendNext:dic];
                    
                 }else{
                
                     [subscriber sendNext:@"网络错误"];

                 }
            
             }
             if (connectionError) {
                  [subscriber sendNext:@"网络错误"];
             }
        
        
         }];
         return nil;
     }];
}
    
@end
